package com.lvmama.pangolin.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.entity.Config;
import com.lvmama.pangolin.entity.ConfigListConditionVO;
import com.lvmama.pangolin.entity.LogListConditionVO;
import com.lvmama.pangolin.entity.LogVO;
import com.lvmama.pangolin.entity.Timer;
import com.lvmama.pangolin.enums.CommonStringType;
import com.lvmama.pangolin.service.config.BackConfigService;

@Controller
@RequestMapping("backConfig")
public class BackGroundController {

	private static Logger log = Logger.getLogger(BackGroundController.class);
	
	@Autowired 
	private BackConfigService configService;
	
    @RequestMapping("logList")
    public String logList(ModelMap model ,LogListConditionVO logListConditionVO) throws Exception{
        CommonStringType[] getErrorLogType = CommonStringType.getErrorLogType(); 
        //错误日志类型列表
        List<CommonStringType> errorLogList = Arrays.asList(getErrorLogType);
        
        CommonStringType[] getBlackIPType = CommonStringType.getBlackIPType();
        //竞品公司代号下拉列表
        List<CommonStringType> blackIpTypeList = Arrays.asList(getBlackIPType);     
        
        PageInfo<LogVO> page = configService.getLogList(logListConditionVO);
        List<LogVO> logVOList = page.getList();
        //分页
        model.addAttribute("page", page);
        model.addAttribute("logVOList", logVOList);
        model.addAttribute("errorLogList", errorLogList);
        model.addAttribute("blackIpTypeList", blackIpTypeList);
        model.addAttribute("logListConditionVO",logListConditionVO);

        return "background/config/logList";
    }
    @RequestMapping("findLogByID")
    @ResponseBody
    public Map<String, Object> findLogByID(ModelMap model ,LogVO logVO) throws Exception{
    	Map<String, Object> map = new HashMap<String, Object>(1);
    	int code = 1;
		try {
			logVO = configService.getLogByIds(logVO);
			
		}catch (Exception e) {
			log.info("backConfigController...findLogByID memthod error");
			code = 0;
			e.printStackTrace();
		}
		map.put("code",code);
		map.put("LogVO",logVO);
       
		return map;
    }
    
    
    @RequestMapping("configList")
    public String configList(ModelMap model ,ConfigListConditionVO configListConditionVO) throws Exception{
    	 PageInfo<Config> page = configService.getConfigList(configListConditionVO);
         List<Config> configVOList = page.getList();
         model.addAttribute("page", page);
         model.addAttribute("configVOList",configVOList);
         model.addAttribute("configListConditionVO",configListConditionVO);
    	 return "background/config/configList";
    }
    
    
	@RequestMapping(value = "/configEdit", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
    public Map<String, Object> configEdit(ModelMap model ,Config config){
    	Map<String, Object> map = new HashMap<String, Object>(1);
    	int code = 1;
    	List<Config> configVOList;
		try {
			configVOList = configService.getConfigByIds(config);
			 if(configVOList!=null && configVOList.size()>0){
	    	     config =configVOList.get(0); 
	    	 }
	        
		} catch (Exception e) {
			code = 0;
			e.printStackTrace();
			log.info("BackGroundController ... method configEdit error");
		}
		map.put("code",code);
		map.put("configVO",config);
		
		return map;
       
    }
    
    @RequestMapping("configAdd")
    public String configAdd(ModelMap model ,Config config){
    	List<Config> configVOList = new ArrayList<Config>(); 
    	ConfigListConditionVO configListConditionVO = new ConfigListConditionVO();
    	PageInfo<Config> page = new PageInfo<Config>();
    	try {
    		 if(config.getConfigid()!=null){
    			 configService.updateConfig(config);
    		 }else{
			   configService.insertConfig(config);
    		 }
	    	 page = configService.getConfigList(configListConditionVO);
	         configVOList = page.getList();
		} catch (Exception e) {
			e.printStackTrace();
			log.info("BackGroundController ... method configAdd error");
		}
    	 model.addAttribute("page", page);
         model.addAttribute("configVOList",configVOList);
         model.addAttribute("configListConditionVO",configListConditionVO);
    	 return "background/config/configList";
    }
    
    
    @RequestMapping("configDelete")
    @ResponseBody
    public Map<String, Object> configDelete(ModelMap model ,Config config) throws Exception{
    	Map<String, Object> map = new HashMap<String,Object>();
    	 try{
    	  configService.deleteConfig(config);
    	  map.put("code", "1");
    	 }catch(Exception e){
    		 map.put("code", "0");
    		 log.info("BackGroundController ... method configDelete error" + e); 
    	 }
    	 ConfigListConditionVO configListConditionVO = new ConfigListConditionVO();
     	 PageInfo<Config> page = configService.getConfigList(configListConditionVO);
         List<Config> configVOList = page.getList();
         model.addAttribute("page", page);
         model.addAttribute("configVOList",configVOList);
         model.addAttribute("configListConditionVO",configListConditionVO);
    	return map;
    }
   
   

}
